﻿# -*- coding=utf-8 -*-
# 官方网站：http://www.vnpy.cn
from ctypes import *
import os.path

global Index
from vnctpmdType import *


class vnklineservice(object):
    def __init__(self):
        currpath = os.path.abspath(os.path.dirname(__file__))
        self.vnkline = CDLL(currpath + '\\vnklineservice.dll')

        self.fGetServerKline = self.vnkline.GetServerKline
        self.fGetServerKline.argtypes = [c_void_p, c_int32]
        self.fGetServerKline.restype = c_int32

    def GetServerKline(self, InstrumentID, TradeingDay):
        try:
            thisInstrumentID = VNInstrument()
            thisInstrumentID.InstrumentID = bytes(InstrumentID, encoding="utf-8")
            self.fGetServerKline(byref(thisInstrumentID), TradeingDay)
            #return cast(data, POINTER(KLineDataType))
            return 1
        except Exception as e:
            print("GetKline Error:" + repr(e))
            return 0

    # 行情回调
    def OnKline(self, a):
        pass

    # 注册行情回调
    def VNRegOnKline(self):
        CMPFUNC = CFUNCTYPE(None, POINTER(KLineDataType))
        self.vnkline.VNRegOnKline(CMPFUNC(self.OnKline))
